import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import getPageTitle from './utils/get-page-title'
import { asyncRoutes } from '@/router/index'
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  document.title = getPageTitle(to.meta.title)
  NProgress.start() // 开启进度条
  const token = store.state.user.token
  if (token) {
    console.log('已经登录')
    // 判断是否去的是登录页
    if (to.path === '/login') {
      // 强制留在诸主页
      next('/') // next指定跳转路径 就不会经过afterEach   next()直接放行才会经过afterEach
      NProgress.done()
    } else {
      // 去的不是登录页 直接放行
      // 保存用户个人信息到vuex
    // 判断是否已经获取用户信息 如果没有获取则获取 已经获取则不再需要获取
      // console.log(store.state.user.userInfo.userId)
      if (!store.state.user.userInfo.userId) {
        // 调用函数获取用户信息
        // 用户的页面权限信息
        const menus = await store.dispatch('user/setUserInfo')
        console.log(menus, 'menus')
        // console.log(asyncRoutes, 'asyncRoutes')
        const filterRouters = asyncRoutes.filter(router => {
          // console.log(router, 'router')
          return menus.includes(router.children[0].name)
        })
        filterRouters.push({ path: '*', redirect: '/404', hidden: true })
        // 动态添加用户可以访问的路由
        router.addRoutes(filterRouters)
        // 把动态路由存储到vuex中
        store.commit('menus/setMenus', filterRouters)
        next({
          ...to, // 保证路由添加完了再进入页面
          replace: true // 重进一次，不保留重复历史
        })
      } else {
        next()
      }
    }
  } else {
    // 没有登录
    // 判断是否去的是白名单
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

// 页面跳转完成会经过这个钩子函数
router.afterEach(() => {
  console.log('跳转完成经过')
  NProgress.done() // 关闭精度条
})

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
